using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class forums_controls_administration_ModifyForum : Controls.DataManagerForumControl
{
    public event EventHandler ForumUpdated;

    protected override void ItemChanged()
    {
        UpdateForm();
    }

    protected void NewForumSelected(object sender, Controls.ForumSelectedArgs e)
    {
        CurrentForum = e.Forum;
        MoveForum1.CurrentForum = CurrentForum;
    }

    private void Refresh()
    {
        ddlForums.Refresh();
        ddlRoot.Refresh();
    }

    private void UpdateForm()
    {
        if (CurrentForum != null)
        {
            pnlForum.Visible = true;
            txtName.Text = CurrentForum.Name;
            txtDescription.Text = CurrentForum.Description;
            ddlRoot.CurrentForum = CurrentForum.Parent;
            switch (CurrentForum.MinimumRequiredStatus)
            {
                case DAL.User.UserStatus.Administrator:
                    ddlVisibility.SelectedValue = "4";
                    break;
                case DAL.User.UserStatus.SuperModerator:
                    ddlVisibility.SelectedValue = "3";
                    break;
                case DAL.User.UserStatus.Moderator:
                    ddlVisibility.SelectedValue = "2";
                    break;
                case DAL.User.UserStatus.Member:
                    ddlVisibility.SelectedValue = "1";
                    break;
                default:
                    ddlVisibility.SelectedValue = "0";
                    break;
            }
        }
        else
        {
            pnlForum.Visible = false;
        }
    }

    protected void ForumMoved(object sender, EventArgs e)
    {
        Refresh();
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            CurrentForum.Name = txtName.Text;
            CurrentForum.Description = txtDescription.Text;
            CurrentForum.Parent = ddlRoot.CurrentForum;
            switch (ddlVisibility.SelectedValue)
            {
                case "4":
                    CurrentForum.MinimumRequiredStatus = DAL.User.UserStatus.Administrator;
                    break;
                case "3":
                    CurrentForum.MinimumRequiredStatus = DAL.User.UserStatus.SuperModerator;
                    break;
                case "2":
                    CurrentForum.MinimumRequiredStatus = DAL.User.UserStatus.Member;
                    break;
                case "1":
                    CurrentForum.MinimumRequiredStatus = DAL.User.UserStatus.Member;
                    break;
                default:
                    CurrentForum.MinimumRequiredStatus = DAL.User.UserStatus.Unregistered;
                    break;
            }

            DAL.Forums.Save(CurrentForum);
            if (ForumUpdated != null)
            {
                ForumUpdated(this, new EventArgs());
            }
        }
    }
    protected void CheckCircularReference(object source, ServerValidateEventArgs args)
    {
        args.IsValid = !CurrentForum.ContainsChildForum(ddlRoot.CurrentForum);
        if (args.IsValid == false)
        {
            if (ddlRoot.CurrentForum != null && CurrentForum != null)
            {
                if (ddlRoot.CurrentForum == CurrentForum)
                {
                    valCircularReference.ErrorMessage = "Parenting a forum to itself creates a circular reference.";
                }
                else
                {
                    valCircularReference.ErrorMessage = String.Format("'{0}' is a child of '{1}'. Parenting '{1}' to '{0}' creates a circular reference.",
                        Server.HtmlEncode(ddlRoot.CurrentForum.Name),
                        Server.HtmlEncode(CurrentForum.Name));
                }
            }
            else
            {
                valCircularReference.ErrorMessage = "Circular reference.";
            }
        }
    }
}
